package com.neu.view.impl;

import com.neu.dao.BillDao;
import com.neu.dao.MemberDao;
import com.neu.dao.impl.BillDaoImpl;
import com.neu.dao.impl.MemberDaoImpl;
import com.neu.view.UserView;
import static com.neu.util.Read.*;

import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class MemberView extends UserView {
    private MemberDao memberDao=new MemberDaoImpl();
    private BillDao billDao = new BillDaoImpl();
    @Override
    public Map<String, Object> login() {
        Map<String,Object> map =null;
        Scanner input=new Scanner(System.in);
        System.out.print("成员名称：");
        String name =input.next();
        System.out.print("请输入密码：");
        String pwd =input.next();
        map =memberDao.login(name,pwd);
        return map;
    }

    @Override
    public void menu() {
        A:while (true) {
            System.out.println("成员记账管理：");
            System.out.println("1.查找账单");
            System.out.println("2.添加账单");
            System.out.println("3.删除账单");
            System.out.println("4.修改账单");
            System.out.println("5.账单总和");
            System.out.println("6.注销");
            System.out.print("请选择：");
            int n=getIn().nextInt();
            switch (n){
                case 1:
                    searchBillMenu();
                    break;
                case 2:
                    insertBill();
                    break;
                case 3:
                    deleteBill();
                break;
                case 4:
                    updateBill();
                    break;
                case 5:
                    countMenu();
                    break;
                case 6:
                    break A;
                default:
                    System.out.println("请正确选择!");
                    break;
            }
        }

    }

    private void countMenu() {
        System.out.println("1.总收入");
        System.out.println("2.总支出");
        System.out.println("3.返回");
        System.out.print("请选择：");
        int n=getIn().nextInt();
        switch (n){
            case 1:
                countIncome();
                break;
            case 2:
                countConsume();
                break;
            case 3:
                break;
            default:
                System.out.println("请正确输入!");


        }
    }

    private void countConsume() {
        List<Map<String,Object>> list =billDao.countConsume();
        System.out.println("总支出");
        for (int j = 0;j<list.size();j++){
            Map<String,Object> map = list.get(j);
            Object count = map.get("sum(bill_money)");
            System.out.println(count);
        }
    }

    private void countIncome() {
        List<Map<String,Object>> list =billDao.countIncome();
        System.out.println("总收入");
        for (int j = 0;j<list.size();j++){
            Map<String,Object> map = list.get(j);
            Object count = map.get("sum(bill_money)");
            System.out.println(count);
        }
    }

    private void updateBill() {
        int row=0;
        System.out.print("请输入被修改的账单编号：");
        String id = getIn().next();
        System.out.print("请输入修改后的钱数：");
        String money=getIn().next();
        String id2 = getInfo().get("member_id").toString();
        System.out.println("是否确认修改？");
        System.out.println("1.确认修改");
        System.out.println("2.返回");
        System.out.print("请选择：");
        int x=getIn().nextInt();
        if (x==1) {
            row = billDao.update(money,id,id2);
        }
        if (row>0){
            System.out.println("修改成功!");
            System.out.println();
        }
    }

    private void deleteBill() {
        int row=0;
        System.out.println("请输入要删除的账单编号");
        String id=getIn().next();
        String id2 = getInfo().get("member_id").toString();
        System.out.println("是否确认删除？");
        System.out.println("1.确认删除");
        System.out.println("2.返回");
        System.out.print("请选择：");
        int x=getIn().nextInt();
        if (x==1) {
            row = billDao.deleteBill(id,id2);
        }
        if (row>0){
            System.out.println("删除成功!");
            System.out.println();
        }
    }

    private void insertBill() {
        int row=0;
        String id = getInfo().get("member_id").toString();
        System.out.print("请输入添加的账单金额：");
        String money = getIn().next();
        System.out.print("请输入添加的账单说明：");
        String explain =getIn().next();
        row = billDao.insertBill(money,explain,id);
        if (row>0){
            System.out.println("添加成功!");
            System.out.println();
        }
    }

    private void searchBillMenu() {
        System.out.println("请选择查询方式：");
        System.out.println("1.查询所有账单");
        System.out.println("2.查询所有支出");
        System.out.println("3.查询所有收入");
        System.out.println("4.按照个人查询账单");
        System.out.println("5.按照日期查询账单");
        System.out.println("6.返回");
        System.out.print("请选择：");
        int n=getIn().nextInt();
        switch (n){
            case 1:
                //商品被删除的不列，如果列出了，status为1显示有货，status为0表示售空
                searchBillAll();
                break;
            case 2:
                searchIncomeBill();
                break;
            case 3:
                searchConsumeBill();
                break;
            case 4:
                searchMemberBill();
                break;
            case 5:
                searchBillByDate();
            case 6:
                break;
            default:
                System.out.println("请正确输入!");
                System.out.println();
                break;
        }
    }

    private void searchBillByDate() {
        System.out.print("请输入账单开始时间：");
        String date1= getIn().next();
        System.out.print("请输入账单结束时间：");
        String date2=getIn().next();
        List<Map<String,Object>> list=billDao.findBillByDate(date1,date1);
        System.out.println("账单编号\t账单金额\t\t\t账单时间\t\t\t账单说明\t账单归属");
        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = list.get(i);
            Object id =map.get("bill_id");
            Object money =map.get("bill_money");
            Object time =map.get("bill_time");
            Object explain = map.get("bill_explain");
            Object member_id =map.get("bill_member_id");
            System.out.println("\t"+id+"\t"+money+"\t"+time+"\t\t"+explain+"\t"+member_id);
        }
    }

    private void searchMemberBill() {
        System.out.println("请输入要查找成员的id：");
        String id1=getIn().next();
        List<Map<String,Object>> list=billDao.findMemberBill(id1);
        System.out.println("账单编号\t账单金额\t\t\t账单时间\t\t\t账单说明\t账单归属");
        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = list.get(i);
            Object id =map.get("bill_id");
            Object money =map.get("bill_money");
            Object time =map.get("bill_time");
            Object explain = map.get("bill_explain");
            Object member_id =map.get("bill_member_id");
            System.out.println("\t"+id+"\t"+money+"\t"+time+"\t\t"+explain+"\t"+member_id);
        }
    }

    private void searchConsumeBill() {
        List<Map<String,Object>> list=billDao.findConsumeBill();
        System.out.println("账单编号\t账单金额\t\t\t账单时间\t\t\t账单说明\t账单归属");
        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = list.get(i);
            Object id =map.get("bill_id");
            Object money =map.get("bill_money");
            Object time =map.get("bill_time");
            Object explain = map.get("bill_explain");
            Object member_id =map.get("bill_member_id");
            System.out.println("\t"+id+"\t"+money+"\t"+time+"\t\t"+explain+"\t"+member_id);
        }
    }

    // 查询总收入
    private void searchIncomeBill() {
        List<Map<String,Object>> list=billDao.findIncomeBill();
        System.out.println("账单编号\t账单金额\t\t\t账单时间\t\t\t账单说明\t账单归属");
        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = list.get(i);
            Object id =map.get("bill_id");
            Object money =map.get("bill_money");
            Object time =map.get("bill_time");
            Object explain = map.get("bill_explain");
            Object member_id =map.get("bill_member_id");
            System.out.println("\t"+id+"\t"+money+"\t"+time+"\t\t"+explain+"\t"+member_id);
        }
    }

    private void searchBillAll() {
        List<Map<String,Object>> list=billDao.findAll();
        System.out.println("账单编号\t账单金额\t\t\t账单时间\t\t\t账单说明\t账单归属");
        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = list.get(i);
            Object id =map.get("bill_id");
            Object money =map.get("bill_money");
            Object time =map.get("bill_time");
            Object explain = map.get("bill_explain");
            Object member_id =map.get("bill_member_id");
            System.out.println("\t"+id+"\t"+money+"\t\t"+time+"\t\t"+explain+"\t"+member_id);
        }
    }
}
